package com.scy.util;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ExceptionHandle implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();
        if(e instanceof IndexOutOfBoundsException){
            mv.addObject("msg","索引越界异常"+e.getMessage());
        }else if(e instanceof NullPointerException){
            mv.addObject("msg","空指针异常"+e.getMessage());
        }else if(e instanceof ArithmeticException){
            mv.addObject("msg","数字转换异常"+e.getMessage());
        }else {
            mv.addObject("msg","其他异常"+e.getMessage());
        }
        mv.setViewName("/error");
        return mv;
    }
}
